Is there a way to combine page object gem and javascript calls
NickName:charlietaylor Ask DateTime:2014-10-14T20:55:07

Is there a way to combine page object gem and javascript calls

Im using the page object gem and selenium,

when filling in a sign up form, the form fills in correctly, but when clicking apply it errors saying the fields are required even though they are filled in.

this seems to be caused because the page object/selenium method isn't firing the javascript change method which is needed for the application to know the field has been filled in

this can be fixed by using code such as

on(SettingsPage).payment_method_account_number = number
@browser.execute_script("$('input[name=account_number]').change()")

but this is obviously not ideal and breaks the whole point of using page object in the first place by having to declare the fields name attribute again

is there a way better way to solve this problem than what i have shown?

Copyright Notice:Content Author:「charlietaylor」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/26361397/is-there-a-way-to-combine-page-object-gem-and-javascript-calls

Answers
Justin Ko 2014-10-14T16:24:13

To avoid duplicating an element definition within the page object as well as the execute_script script, you can pass the page object element to the script.\n\nThe underlying Selenium-WebDriver (and therefore the Page-Object gem) supports an arguments array within the script being executed. This arguments array basically takes a Selenium-WebDriver elements and converts them to something usable by the script. The Page-Object execute_script method handles the conversion of elements to the right type, so you simply need to:\n\n\nDeclare a script that uses the arguments array\nPass in a PageObject::Element\n\n\nFor example, let us assume your page object has used the accessor:\n\ntext_field(:payment_method_account_number, :name => 'account_number')\n\n\nTherefore, the page object will have a payment_method_account_number_element method that returns the PageObject::Element for this text field. \n\nWithin the script you want to execute, you can replace how you locate the element with the arguments array and pass in the PageObject::Element to the execute_script method:\n\nexecute_script(\"$(arguments[0]).change();\", payment_method_account_number_element)\n\n\nThen you can re-write the call to on as:\n\non(SettingsPage) do |page|\n page.payment_method_account_number = number\n page.execute_script(\"$(arguments[0]).change();\", page.payment_method_account_number_element)\nend\n\n\n(Or, as Dane pointed out, put this into a method in the page object.)",


More about “Is there a way to combine page object gem and javascript calls” related questions